package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.constant.Constant;
import com.lpit.dto.HomePageDto;
import com.lpit.mapper.ArticleMapper;
import com.lpit.result.ResponseResult;
import com.lpit.service.FindArticlesThatUsersWillLikeService;
import com.lpit.vo.ArticleVo;
import com.lpit.vo.HomePageDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class FindArticlesThatUsersWillLikeServiceImpl implements FindArticlesThatUsersWillLikeService {

    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 找到用户会喜欢的文章
     * @param homePageDto
     * @return ResponseResult<List<HomePageDto>>
     */
    @Override
    public ResponseResult<List<HomePageDataVo>> findArticlesThatUsersWillLike(HomePageDto homePageDto) {
        if(null == homePageDto){
            return new ResponseResult<>(400,null,Constant.NO_CONDITIONAL_INFORMATION);
        }
        if(null == homePageDto.getUserId() || 0 == homePageDto.getUserId()){
            return new ResponseResult<>(401,null,Constant.THE_USER_WAS_NOT_FOUND);
        }
        if(null == homePageDto.getChannelName() || "".equals(homePageDto.getChannelName())){
            /* 默认为推荐频道 */
            homePageDto.setChannelName(Constant.RECOMMENDED);
        }
        if(null == homePageDto.getChannelName() || "".equals(homePageDto.getNewestTime())){
            /* 默认获取当前的系统时间 */
            homePageDto.setNewestTime(new SimpleDateFormat(Constant.TIME_FORMAT).format(new Date()));
        }
        if(null == homePageDto.getSize() || 0 == homePageDto.getSize()){
            /* 设置默认值 */
            homePageDto.setSize(10);
        }
        IPage<HomePageDataVo> articleVoIPage = new Page<>(0, homePageDto.getSize());
        List<HomePageDataVo> homePageDataVos = articleMapper.selectFindArticlesThatUsersWillLike(articleVoIPage, homePageDto.getUserId());
        if(null == homePageDataVos || 0 == homePageDataVos.size()){
            return new ResponseResult<>(204,homePageDataVos,Constant.NO_RELATED_ARTICLE_FOUND);
        }
        return new ResponseResult<>(homePageDataVos,Constant.SUCCESS);
    }
}
